package com.runvp.common.enums;

/**
 * 服务指令
 *
 */
public enum NettyCmd implements BaseEnumInterface{
    SEND(NettyType.DEVICE_TO_SERVER.getCode(), "D6DADEDB", "发起/应答"),
    OUT(NettyType.DEVICE_TO_SERVER.getCode(), "021", "服务器下发收费方案"),

    LOGIN(NettyType.DEVICE_TO_SERVER.getCode(), "01", "设备请求登录"),
    SERVER_RES_LOGIN(NettyType.SERVER_RECEIVE_DEVICE.getCode(), "01", "服务器登录应答"),
    DEVICE_REQ_FEE_SCHEME(NettyType.DEVICE_TO_SERVER.getCode(), "02", "设备请求收费方案"),
    SERVER_RES_FEE_SCHEME(NettyType.SERVER_RECEIVE_DEVICE.getCode(), "02", "服务器应答收费方案"),
    SERVER_SEND_FEE_SCHEME(NettyType.SERVER_TO_DEVICE.getCode(), "02", "服务器下发收费方案"),
    DEVICE_RES_FEE_SCHEME(NettyType.DEVICE_RECEIVE_SERVER.getCode(), "02", "设备应答收费方案"),

    XL(NettyType.DEVICE_TO_SERVER.getCode(), "04", "设备发送心跳保活"),
    SERVER_RES_DEVICE_XL(NettyType.SERVER_RECEIVE_DEVICE.getCode(), "04", "服务器应答心跳"),
    CHARG_START(NettyType.SERVER_TO_DEVICE.getCode(), "05", "服务器启动充电"),
    DEVICE_CHARG_START_RES(NettyType.DEVICE_RECEIVE_SERVER.getCode(), "05", "启动充电应答"),
    CHARG_STOP(NettyType.SERVER_TO_DEVICE.getCode(), "06", "服务器停止充电"),
    DEVICE_CHARG_STOP_RES(NettyType.DEVICE_RECEIVE_SERVER.getCode(), "06", "停止充电应答"),
    CHARG_END_REPORT(NettyType.DEVICE_TO_SERVER.getCode(), "07", "设备充电结束上报"),
    SERVER_RES_CHARG_END_REPORT(NettyType.SERVER_RECEIVE_DEVICE.getCode(), "07", "服务器应答充电结束上报"),
    DEVICE_REQ_CARD_BALANCE(NettyType.DEVICE_TO_SERVER.getCode(), "08", "设备刷卡请求余额"),
    SERVER_CARD_BALANCE(NettyType.SERVER_RECEIVE_DEVICE.getCode(), "08", "服务器响应卡余额"),
    DEVICE_SWIPE_CARD_REQ_CHARGE(NettyType.DEVICE_TO_SERVER.getCode(), "09", "设备刷卡请求充电"),
    SERVER_RES_SWIPE_CARD_CHARGE(NettyType.SERVER_RECEIVE_DEVICE.getCode(), "09", "服务器应答刷卡充电"),
    CHARGING_REAL_TIME_DATA_REPORT(NettyType.DEVICE_TO_SERVER.getCode(), "0A", "充电过程中实时数据上报"),
    SERVER_RES_CHARGING_REAL_TIME_DATA_REPORT(NettyType.DEVICE_TO_SERVER.getCode(), "0A", "服务器应答充电过程中实时数据"),
    SERVER_CONFIG_DEVICE_WORK_PARAM(NettyType.SERVER_TO_DEVICE.getCode(), "0B", "服务器设置设备工作参数"),
    DEVICE_RES_WORK_PARAM_CONFIG(NettyType.DEVICE_RECEIVE_SERVER.getCode(), "0B", "设备应答工作参数设置"),
    SERVER_QUERY_DEVICE_WORK_PARAM(NettyType.SERVER_TO_DEVICE.getCode(), "0C", "服务器查询设备工作参数"),
    DEVICE_RES_WORK_PARAM(NettyType.DEVICE_RECEIVE_SERVER.getCode(), "0C", "设备应答工作参数"),
    SERVER_SEND_OTA_CMD(NettyType.SERVER_TO_DEVICE.getCode(), "0D", "服务下发 OTA 指令"),
    DEVICE_RES_OTA_CMD(NettyType.DEVICE_RECEIVE_SERVER.getCode(), "0D", "设备应答 OTA 指令"),
    DEVICE_REPORT_BASE_INFO(NettyType.DEVICE_TO_SERVER.getCode(), "0E", "设备上报基本信息"),
    SERVER_RES_BASE_INFO(NettyType.SERVER_RECEIVE_DEVICE.getCode(), "0E", "服务器应答基本信息"),
    SERVER_QUERY_REAL_TIME_INFO(NettyType.SERVER_TO_DEVICE.getCode(), "0F", "服务器查询实时信息"),
    DEVICE_RES_REAL_TIME_INFO(NettyType.DEVICE_RECEIVE_SERVER.getCode(), "0F", "设备应答实时信息"),
    DEVICE_TIMING_REPORT_REAL_TIME_INFO(NettyType.DEVICE_TO_SERVER.getCode(), "0F", "设备定时上报实时信息"),
    SERVER_RES_DEVICE_REAL_TIME_REPORT_INFO(NettyType.SERVER_RECEIVE_DEVICE.getCode(), "0F", "服务器应答设备上报实时信息"),
    SERVER_REMOTE_RESTART(NettyType.SERVER_TO_DEVICE.getCode(), "10", "服务器远程重启"),
    DEVICE_RES_REMOTE_RESTART(NettyType.DEVICE_RECEIVE_SERVER.getCode(), "10", "设备应答远程重启"),
    SERVER_QUERY_PORT_RECHARGEABLE_STATUS(NettyType.SERVER_TO_DEVICE.getCode(), "12", "服务器查询端口可充电状态"),
    DEVICE_RES_PORT_RECHARGEABLE_STATUS(NettyType.DEVICE_RECEIVE_SERVER.getCode(), "12", "设备应答端口可充电状态"),
    SERVER_RES_PORT_RECHARGEABLE_STATUS(NettyType.SERVER_RECEIVE_DEVICE.getCode(), "12", "服务器应答端口可充电状态反馈"),
    ON_OFF("03", "开/关");
    private String type;    //类型
    private String code;    //cmd
    private String desc;    //描述

    public static NettyCmd build(String code){
        if(null == code || "".equals(code)){return  null ;}
        for (NettyCmd e: NettyCmd.values()){
            if(code.equals(e.getCode())){
                return e;
            }
        }
        return null;
    }
    public static NettyCmd build(String type,String code){
        if(null == code || "".equals(code)){return  null ;}
        for (NettyCmd e: NettyCmd.values()){
            if(type.equals(e.getType()) && code.equals(e.getCode())){
                return e;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        System.out.println(build("03","02"));
    }
    /**
     * 获取指令
     * @param code
     * @param ignoreXL 是否忽略心跳 true :是
     * @return NettyCmd
     */
    public static NettyCmd build(String code,boolean ignoreXL){
        if(null == code || "".equals(code)){return  null ;}
        for (NettyCmd e: NettyCmd.values()){
            if(code.equals(e.getCode())){
                if(ignoreXL){
                    continue;
                }
                return e;
            }
        }
        return null;
    }
    private NettyCmd(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    NettyCmd(String type, String code, String desc) {
        this.type = type;
        this.code = code;
        this.desc = desc;
    }

    public String getCode() {
        return code;
    }

    @Override
    public String getDesc() {
        return desc;
    }

    @Override
    public String getDefaulted() {
        return null;
    }

    public String getType() {
        return type;
    }

    /**
     * 是否包含枚举key值
     *
     * @param key
     * @return boolean true：包含，  false：不包含
     */
    public static boolean isContains(String key) {
        if(null == key || "".equals(key)){return  false ;}
        for (NettyCmd e: NettyCmd.values()){
            if(key.equals(e.getCode())){
                return true;
            }
        }
        return false;
    }

    /**
     * 根据key获取对应 desc
     * @param code
     * @return str
     */
    public static String getDesc(String code) {
        if(null == code || "".equals(code)){return  "未知" ;}
        for (NettyCmd e: NettyCmd.values()){
            if(code.equals(e.getCode())){
                return e.getDesc();
            }
        }
        return "未知";
    }
    /**
     * 根据key获取对应 desc
     * @param type
     * @param code
     * @return str
     */
    public static String getDesc(String type,String code) {
        if(null == code || "".equals(code)){return  "未知" ;}
        for (NettyCmd e: NettyCmd.values()){
            if(type.equals(e.getType()) && code.equals(e.getCode())){
                return e.getDesc();
            }
        }
        return "未知";
    }
}
